﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2009-04-11 20:01:19Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using Outcoder.Gui;
using Outcoder.Services.MessageService;

namespace Outcoder.Services.Implementation
{
	/// <summary>
	/// WPF implementation of the <see cref="IMessageService"/>.
	/// </summary>
	public class MessageService : MessageServiceBase
	{
		public override MessageResult ShowCustomDialog(string message, string caption,
			MessageButton messageButton, MessageImage messageImage, 
			MessageImportance? importanceThreshold, string details)
		{
			/* If the importance threshold has been specified 
			 * and it's less than the minimum level required (the filter level) 
			 * then we don't show the message. */
			if (importanceThreshold.HasValue && importanceThreshold.Value < MinumumImportance)
			{
				return MessageResult.OK;
			}

			Window mainWindow = GetMainWindow();
			Dispatcher dispatcher = mainWindow != null ? mainWindow.Dispatcher : Dispatcher.CurrentDispatcher;

			if (dispatcher == null || dispatcher.CheckAccess())
			{
				MessageBoxResult messageBoxResult;

				if (mainWindow != null)
				{
					/* We are on the UI thread, and hence no need to invoke the call.*/
					messageBoxResult = MessageBox.Show(mainWindow, message, caption, 
					                                   messageButton.TranslateToMessageBoxButton(), 
					                                   messageImage.TranslateToMessageBoxButton());
				}
				else
				{
					messageBoxResult = MessageBox.Show(message, caption,
					   messageButton.TranslateToMessageBoxButton(),
					   messageImage.TranslateToMessageBoxButton());
				}
				
				return messageBoxResult.TranslateToMessageBoxResult();
			}

			MessageResult result = MessageResult.OK; /* Satisfy compiler with default value. */
			dispatcher.Invoke((ThreadStart)delegate
			{
				MessageBoxResult messageBoxResult;

				if (mainWindow != null)
				{
					/* We are on the UI thread, and hence no need to invoke the call.*/
					messageBoxResult = MessageBox.Show(mainWindow, message, caption,
													   messageButton.TranslateToMessageBoxButton(),
													   messageImage.TranslateToMessageBoxButton());
				}
				else
				{
					messageBoxResult = MessageBox.Show(message, caption,
					   messageButton.TranslateToMessageBoxButton(),
					   messageImage.TranslateToMessageBoxButton());
				}
				result = messageBoxResult.TranslateToMessageBoxResult();
			});

			return result;
		}

		public override TextResponse AskQuestionWithTextResponse(TextResponseQuestion textResponseQuestion)
		{
			throw new NotImplementedException();
		}

		static Window GetMainWindow()
		{
			IMainWindow window;
			if (Dependency.TryResolve<IMainWindow>(out window))
			{
				Window result = window as Window;
				if (result != null)
				{
					return result;
				}
			}
			if (Application.Current != null)
			{
				return Application.Current.MainWindow;
			}
			return null;
		}
	}


}
